<template>
	<view>
		<uni-swipe-action>
			<uni-swipe-action-item
				v-for="item in comments"
				:key="item.id"
				:threshold="0"
				:right-options="options"
				@click="(e) => bindClick(e, item)"
			>
				<CommentCard
					:comment-id="item.id"
					:score="item.score"
					:date="item.date"
					:comment="item.comment"
					:liked-count="item.likeCount"
				></CommentCard>
			</uni-swipe-action-item>
		</uni-swipe-action>
	</view>
</template>

<script>
import { useUserStore } from '@/stores/user.js';
import { reqMyComments, reqDeleteCommentById, reqUpdateComment } from '@/apis/comment.js';
export default {
	data() {
		return {
			dishId: '',
			userId: '',
			options: [
				{
					text: '删除',
					style: { backgroundColor: '#F56C6C' },
				},
				// {
				// 	text: '修改',
				// 	style: { backgroundColor: '#007aff' },
				// },
			],

			comments: [
				// {
				// 	id: 1,
				// 	userId: 1,
				// 	dishId: 1,
				// 	date: '2025/01/01 08:22:55',
				// 	score: 3.5,
				// 	comment: 'goodgoodgoodgoodgood',
				// 	likeCount: 30,
				// },
				// {
				// 	id: 2,
				// 	userId: 1,
				// 	dishId: 1,
				// 	date: '2024-11-23',
				// 	score: 4.5,
				// 	comment: 'goodgoodgoodgoodgood',
				// 	likeCount: 125,
				// },
			],
		};
	},
	onLoad(e) {
		// console.log('my comment ', e);
		// console.log('dishId', dishId);
		this.dishId = parseInt(e.dishId) || 0;
		this.userId = useUserStore().id;
		this.getMyComment();
	},
	methods: {
		getMyComment() {
			reqMyComments({
				dishId: this.dishId,
				userId: this.userId,
			}).then((res) => {
				// console.log('getMyComment', res);
				this.comments = res.data.comments;
			});
		},
		bindClick({ position, index }, comment) {
			// ["删除","修改"]
			// console.log(position, index);
			// console.log('comment', comment);
			const that = this;
			// "删除
			if (index === 0) {
				uni.showModal({
					title: '提示',
					content: '确定要删除吗',
					success(res) {
						if (res.confirm) {
							// console.log('确定');
							that.deleteComment(comment.id);
						} else if (res.cancel) {
							console.log('取消');
						}
					},
				});
			} else if (index === 1) {
				// 修改
			}
		},
		// 删除评论
		deleteComment(commentId) {
			uni.showLoading({
				title: '',
			});
			reqDeleteCommentById(commentId)
				.then((res) => {
					uni.showToast({
						title: '删除成功',
						icon: 'success',
					});
					uni.navigateBack();
				})
				.finally(() => {
					uni.hideLoading();
				});
		},
		// 修改评论
		// deleteComment() {},
	},
};
</script>

<style></style>
